<?php

namespace app\admin\controller;

use app\common\controller\BasicAdmin;
use app\common\model\UserModel;
use app\common\traits\SelectPage;
use app\common\model\CourseAttendanceModel;
use app\common\model\CourseModel;
use app\common\model\UserTimesModel;

class Teacher extends BasicAdmin
{
    use SelectPage;
    public $model = 'UserModel';

    /**
     * 教师列表
     */
    public function index()
    {
        return $this->_query($this->model)
            ->equal('school_id')
            ->equal('grade_id')
            ->equal('class_id')
            ->like('realname,phone')
//            ->append(['poster_paylog_total'])
            ->where('role', 2)
            ->page();
    }

    /**
     * 详情
     */
    public function detail()
    {
        $user_id = $this->request->get('id');
        $user =  UserModel::where('id',$user_id)->with(['school','schoolgrade','schoolclass'])->find();
        //常规
        $user['normal'] = CourseModel::where('status',1)
            ->where('type',1)
            ->where('teacher_id',$user['id'])
            ->select();
        //兴趣
        $course_ids = UserTimesModel::where('teacher_id',$user['id'])
            ->field('group_concat(course_id) as course_ids')->find()['course_ids'];
        $user['interest'] = CourseModel::where('status',1)
            ->where('type',2)
            ->where('id','in',$course_ids)
            ->select();
        //考勤
        $attendance = CourseAttendanceModel::where('teacher_id',$user['id'])->select();
        foreach ($attendance as $key=>$value){
            $value['course_name'] = CourseModel::where('id',$value['course_id'])->value('name');
        }
        $user['attendance'] = $attendance;
        //获取教师的授课时长与次数
        $user['count'] = CourseAttendanceModel::where('teacher_id',$user['id'])->count();
        $user['time'] =  ceil(45*$user['count']/60);
        $this->assign('user',$user);
        $this->fetch();
    }

    /**
     * 设置真实姓名
     */
    public function set_realname()
    {
        $this->title = '编辑';
        return $this->_form($this->model, 'realname');
    }
}